from threading import Thread
import time
import threading


global_flag = {}
CRITICAL_THREAD_EXIT = 20

def log(msg):
    timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
    thread_name = threading.current_thread().getName()
    print('[' + timestamp + '][' + thread_name + ']' + msg + '\n')

def do_deal():
    while True:
        global_flag.setdefault(threading.current_thread().name, 0)
        global_flag[threading.current_thread().name] += 1
        log('current_flag:' + str(global_flag[threading.current_thread().name]))
        if global_flag[threading.current_thread().name] > CRITICAL_THREAD_EXIT:
            # stop thread
            print('task queue is empty, and triggered quit signal, bye!')
            break
        else:
            print("task queue is empty, sleep a while.")
            time.sleep(1)
            continue

Thread(target=do_deal, name='producer1').start()
